<?php
/**
 * Created by PhpStorm.
 * Company：深圳市点购电子商务有限公司广州分公司
 * Action:Api基类
 * User: zlq
 * Date: 2019/2/11
 * Time: 14:11
 */
namespace app\common\controller;
use think\facade\Request;


class ApiBase extends Base
{

    public function __construct(Request $request)
    {
        parent::__construct();

    }

    /**
     * 返回格式化后的json响应
     * @param $data
     * @param int $code
     * @param null $msg
     * @return \Illuminate\Http\JsonResponse
     */
    protected function reply($data, $code = 200, $msg = null)
    {
        return $this->replyDirect($this->returnFormat($data, $code, $msg));
    }

    /**
     * 直接返回json响应
     * @param $obj
     * @return \Illuminate\Http\JsonResponse
     */
    protected function replyDirect($obj)
    {
        return json($obj);
    }

    /**
     * 格式化返回
     * @param $data
     * @param int $code
     * @param null $msg
     * @return array
     */
    protected function returnFormat($data, $code = 200, $msg = null)
    {
        $obj = [
            "data" => $data,
            "code" => $code,
        ];
        if ($code != 200 && empty($msg)) {
            $code_msg = lang($code);
            if (!empty($code_msg)) $msg = $code_msg;
        }
        $obj['msg'] = $msg;
        return $obj;
    }

}